<%@page import="java.text.DecimalFormat"%>
<%@page import="org.proddevplm.dao.data.conceptSelection.PughAnalysis"%>
<%@page import="org.proddevplm.constants.ControllerConstants"%>
<%@page import="org.proddevplm.business.tools.constants.ToolsConstants" %>
<%@page import="org.proddevplm.business.activity.constants.ActivityConstants" %>

<script>

	$(function(){
		$("button").button();
		$("#pughResults").hide();
	})
	
	function autoFill(id){
		$('#'+id).autocomplete("search", " ");
	}
	
	$(function() {
		var pughScale = [
			"++ ",
			"+ ",
			"O ",
			"- ",
			"-- ",
		];
		$( ".pughField" ).autocomplete({
			source: pughScale,
		});
	});
	
	function calculatePugh(rows, cols){
		var matrixValues = '';
		for(var i=0; i<rows; i++){
			for(var j=0; j<cols; j++){
				var id = "elem_"+(i+1)+"_"+(j+1);
				var value = document.getElementById(id).value;
				if(value == "++ "){
					value = 3;
				}else if(value == "+ "){
					value = 1;
				}else if(value == "O "){
					value = 0;
				}else if(value =="- "){
					value = -1;
				}else{
					value = -3;
				}
				matrixValues+=value+"~";
			}
		}
		matrixValues = matrixValues.substring(0, matrixValues.length-1);
		
		var arguments = "activityName="+activityName+"&pughMatrixValues="+matrixValues;
		ajaxCall("toolsManager", "25", arguments, showPughResults);
	}
	
	function showPughResults(data){
		$("#pughResults").show();
		var callback = data;
		var all = callback.split("&xkv");
		var positives = all[0].split("~");
		var negatives = all[1].split("~");
		var net = all[2].split("~");
		
		for(var k=0; k<positives.length; k++){
			var positiveVal = parseFloat(positives[k]);
			var positiveResult = Math.round(positiveVal*10000)/100;
			var negativeVal = parseFloat(negatives[k]);
			var negativeResult = Math.round(negativeVal*10000)/100;
			var netVal = parseFloat(net[k]);
			var netResult = Math.round(netVal*10000)/100;
			$("#positive_"+k).html(positiveResult+"%");
			$("#negative_"+k).html(negativeResult+"%");
			$("#net_"+k).html(netResult+"%");
		}
	}
	
	function finishPugh(){
		$("#activityName").val(activityName);
		document.forms["finishActivityForm"].submit();
	}

</script>

<%
	PughAnalysis currentPugh = (PughAnalysis)session.getAttribute("currentPugh");
	String criteriaString = currentPugh.getCriteria();
	String[] criteria = criteriaString.split("~");
	String conceptsString = currentPugh.getConcepts();
	String[] concepts = conceptsString.split("~");
	String criteriaImportances = currentPugh.getCriteriaImportances();
	String[] importances = criteriaImportances.split("~");
%>

<table id="pughTable" border="1" cellspacing="0" cellpadding="3" style="width:50%; font-size:small">
	
	<tr id="headerUp" style="height:60px; background-color:#F0F0F6">
		<td style="font-weight:bold">Concepts</td>
		<%for(int i=0; i<concepts.length; i++){%>
			<td rowspan="2" style="padding:0"><span class="rotatedText" style="margin-bottom:0px; margin:-20px">
				<%=concepts[i] %>
				</span>
			</td>
		<%}%>
		<td rowspan="2" style="font-weight:bold;">Importance</td>
	</tr>
	<tr id="headerDown" style="height:60px; background-color:#F0F0F6">
		<td style="font-weight:bold">Criteria</td>
	</tr>
	<%for(int i=0; i<criteria.length; i++){ %>
		<tr>
			<td><%=criteria[i] %></td>
			<%for(int j=0; j<concepts.length; j++){ %>
				<td style="width:10%">
					<input id="elem_<%=i+1 %>_<%=j+1 %>" class="pughField" style="width:100%; text-align:center; border:none" onclick="autoFill('elem_<%=i+1%>_<%=j+1%>')"/>
				</td>
			<%} %>
			<td>
				<%Float imp = Float.valueOf(importances[i]);
				Float value = imp*100;
				DecimalFormat dFormat = new DecimalFormat("0.00");
				String shownVal = dFormat.format(value)+"%";%>
				<%=shownVal %>
			</td>
		</tr>
	<%} %>
</table>
<div style="width:50%; margin-top:10px">
	<button id="calculatePugh" name="calculatePugh" onclick="calculatePugh('<%=criteria.length%>','<%=concepts.length%>')" style="width:40%; font-size:small; font-weight:bold; float:left">Calculate</button>
</div>
<div style="clear:both"></div>
<div id="pughResults" style="width:50%;">
	<table border="1" cellspacing="0" cellpadding="3" style="width:100%; font-size:small; margin-top: 10px;">
		<tr style="background-color:#F0F0F6">
			<td>&nbsp</td>
			<%for (int k=0; k<concepts.length; k++){%>
				<td><%=concepts[k] %></td>
			<%} %>
		</tr>
		<tr>
			<td style="background-color:#F0F0F6">Positive effects</td>
			<%for(int k=0; k<concepts.length; k++) {%>
				<td id="positive_<%=k %>"></td>
			<%} %>
		</tr>
		<tr>
			<td style="background-color:#F0F0F6">Negative effects</td>
			<%for(int k=0; k<concepts.length; k++) {%>
				<td id="negative_<%=k %>"></td>
			<%} %>
		</tr>
		<tr>
			<td style="background-color:#F0F0F6">Net effects</td>
			<%for(int k=0; k<concepts.length; k++) {%>
				<td id="net_<%=k %>"></td>
			<%} %>
		</tr>
	</table>
	<div style="width:100%; margin-top:10px; float:center">
		<button id="finishPugh" name="finishPugh" onclick="finishPugh()" style="width:40%; font-weight: bold; float:left;">Finish analysis</button>
	</div>
	<form name="finishActivityForm" method="post" action="/proddev/controller">
		<input type = "hidden" name = "<%=ControllerConstants.MANAGER_KEY%>" value = "<%=ControllerConstants.ACTIVITY_MANAGER%>"/>
		<input type = "hidden" name = "<%=ControllerConstants.METHOD_KEY%>" value = "<%=ActivityConstants.CHANGE_STATUS%>"/>
		<input id="activityName" name="activityName" type="hidden"/>
	</form>
	<div style="clear:both"></div>
</div>
<div style="clear:both"></div>